/**
 * Copyright 2009 Emory University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: The 
 * above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql

/**
 * Append a FOREIGN KEY table constraint to a StringBuilder.
 * 
 * @param constraintName
 *      An optional name for the column constraint.
 * @param refTable 
 *      The table referenced by this foreign key
 * @param columnNames
 *      The name(s) of the column(s) in this table that contain the foreign key value
 * @param refColumnNames
 *      The name(s) of the column(s) in the table referenced by the
 */
class ForeignKeyTableConstraint private(val constraintName:Option[String], 
                                        val refTable : TableName,
                                        val columnNames:Seq[String],
                                        val refColumnNames:Seq[String], 
                                        val matchType:Option[ForeignKeyMatch.Value], 
                                        val updateAction:Option[ForeignKeyAction.Value], 
                                        val deleteAction:Option[ForeignKeyAction.Value]) extends ColumnConstraint with References with SQLIdentifier {
  if (refColumnNames.length != 0 && refColumnNames.length != columnNames.length) { 
    throw new IllegalArgumentException("The length of refColumnNames must either be zero or equal to the length of columnNames.")
  }
  
  def append(builder:StringBuilder) {
    if (constraintName.isDefined) {
      appendConstraintName(constraintName, builder)
      builder.append(" ")
    }
    builder.append("FOREIGN KEY(")
    appendIdentifierSeq(columnNames, builder)
    builder.append(")")
    appendReferences(builder)
  }
}

object ForeignKeyTableConstraint {
  def apply(refTable:TableName, columnNames:String*) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, List(), None, None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnNames:String*) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, List(), None, None, None)}

  def apply(refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String]) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, refColumnNames, None, None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String]) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, refColumnNames, None, None, None)}

  def apply(refTable:TableName, columnNames:Seq[String], matchType:ForeignKeyMatch.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, List(), Some(matchType), None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], matchType:ForeignKeyMatch.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, List(), Some(matchType), None, None)}

  def apply(refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], matchType:ForeignKeyMatch.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, refColumnNames, Some(matchType), None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], matchType:ForeignKeyMatch.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, refColumnNames, Some(matchType), None, None)}

  def apply(refTable:TableName, columnNames:Seq[String], updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, List(), None, Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, List(), None, Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, refColumnNames, None, Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, refColumnNames, None, Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, columnNames:Seq[String], matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, List(), Some(matchType), Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, List(), Some(matchType), Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(None, refTable, columnNames, refColumnNames, Some(matchType), Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnNames:Seq[String], refColumnNames:Seq[String], matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ForeignKeyTableConstraint(Some(constraintName), refTable, columnNames, refColumnNames, Some(matchType), Some(updateAction), Some(deleteAction))}
}
